﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using Hikari.Common;
using Hikari.Areas.Administrator.Common;
using Hikari.Areas.Administrator.Models;
using System.Collections;
using System.Data;

namespace Hikari.Areas.Administrator.Common
{
    public class CategoriesDB
    {
        DataTable dt = new DataTable();
        public int Count;
        public string CreateString = string.Empty;        
        public List<Category> List()
        {
            try
            {
                SapphireDataContext sa = new SapphireDataContext();
                var item = (from a in sa.Categories orderby a.Ordinal select a).ToList();
                return item;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public iCategory Get(int id)
        {
            try
            {
                iCategory iCate = new iCategory();
                SapphireDataContext sa = new SapphireDataContext();
                var item = (from a in sa.Categories where a.ID == id select a).Single();
                iCate.ID = item.ID;
                iCate.Name = item.Name;
                iCate.Descriptions = item.Descriptions;
                iCate.Type = item.Type;
                iCate.Tag = item.Tag;
                iCate.Avatar = item.Avatar;                
                iCate.Enable = item.Enable;                
                iCate.MetaDescription = item.MetaDescription;
                iCate.MetaKeywords = item.MetaKeywords;
                iCate.MetaTitle = item.MetaTitle;                
                iCate.Ordinal = item.Ordinal;
                iCate.ParentID = item.ParentID;
                iCate.Prefix = item.Prefix;
                iCate.PriorityShow = item.PriorityShow;             
                return iCate;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool Creates(iCategory item)
        {
            try
            {
                SapphireDataContext sa = new SapphireDataContext();
                Category ca = new Category();
                ca.Avatar = item.Avatar;
                ca.Descriptions = item.Descriptions;
                ca.Enable = item.Enable;
                ca.ID = item.ID;
                ca.MetaDescription = item.MetaDescription;
                ca.MetaKeywords = item.MetaKeywords;
                ca.MetaTitle = item.MetaTitle;
                ca.Name = item.Name;
                ca.Ordinal = item.Ordinal;
                ca.ParentID = item.ParentID;
                ca.Prefix = item.Prefix;
                ca.PriorityShow = item.PriorityShow;
                ca.RecycleBin = false;
                //ca.RecycleBinDate = (DateTime?)DateTime.Now();
                ca.Tag = item.Tag;
                ca.Type = item.Type;
                sa.Categories.InsertOnSubmit(ca);
                sa.SubmitChanges();
                
            }
            catch (Exception ex)
            {
                
            }
            return false;
        }
        public bool Edit(iCategory item)
        {
            try
            {
                SapphireDataContext sa = new SapphireDataContext();
                var category = (from a in sa.Categories where a.ID == item.ID select a).Single();
                category.Name = item.Name;
                //-----------------
                sa.SubmitChanges();

            }
            catch (Exception ex)
            {

            }
            return false;
        }
        public bool Del(int id)
        {
            try
            {
                SapphireDataContext sa = new SapphireDataContext();
                IEnumerable<Category> cate = from a in sa.Categories where a.ID == id select a;
                sa.Categories.DeleteAllOnSubmit(cate);
                sa.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }            
        }
        public string DisplayCate()
        {            
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Descriptions", typeof(string)));
            dt.Columns.Add(new DataColumn("Prefix", typeof(string)));
            dt.Columns.Add(new DataColumn("Type", typeof(string)));
            dt.Columns.Add(new DataColumn("Enable", typeof(string)));
            dt.Columns.Add(new DataColumn("ParentID", typeof(int)));
            SapphireDataContext sa = new SapphireDataContext();
            var item = (from a in sa.Categories orderby a.Ordinal select a).ToList();            
            foreach (var i in item)
            {
                DataRow row = dt.NewRow();
                row[0] = i.ID;
                row[1] = i.Name;
                row[2] = i.Descriptions;
                row[3] = i.Prefix;
                string t = "";
                if (i.Type == 1)
                {
                    t = "Tin tức";
                }
                else if (i.Type == 2)
                {
                    t = "Sản phẩm";
                }
                row[4] = t;
                string e = "";
                if (i.Enable)
                {
                    e = "Bật";
                }
                else
                {
                    e = "Tắt";
                }
                row[5] = e;
                row[6] = i.ParentID;
                dt.Rows.Add(row);
            }
            string Strings = "<table class=\"widefat tag fixed\" cellspacing=\"0\">";
            Strings += "<thead><tr><th scope='col' id='name' class='manage-column column-name' style=''>Tên</th><th scope='col' id='description' class='manage-column column-description' style=''>Mô tả</th><th scope='col' id='slug' class='manage-column column-slug' style=''>Đường dẫn</th><th scope='col' class='manage-column column-slug' style=''>Thể loại</th><th scope='col' id='posts' class='manage-column column-posts num' style=''>Trạng thái</th></tr></thead>";
            Strings += "<tfoot><tr><th scope='col' id='name' class='manage-column column-name' style=''>Tên</th><th scope='col' id='description' class='manage-column column-description' style=''>Mô tả</th><th scope='col' id='slug' class='manage-column column-slug' style=''>Đường dẫn</th><th scope='col' class='manage-column column-slug' style=''>Thể loại</th><th scope='col' id='posts' class='manage-column column-posts num' style=''>Trạng thái</th></tr></tfoot>";
            Strings += CreateCate("0");
            Strings += "</table>";
            return Strings;
        }
        public string CreateCate(object str)
        {
            DataRow[] drClass = dt.Select(string.Format("ParentID='{0}'", str));            
            foreach (DataRow dr in drClass)
            {
                Count = (dt.Select(string.Format("ParentID='{0}'", dr["ID"]))).Length;                
                CreateString += "<tr>";
                CreateString += "<td class=\"name column-name\">";
                CreateString += "<strong><a class=\"row-title\" href=\"#\">";
                if (dr["ParentID"].ToString() != "0")
                    CreateString += " - " + dr["Name"].ToString();
                else
                    CreateString += dr["Name"].ToString();
                CreateString += "</a> (" + Count + ")</strong><br><div class=\"row-actions\">";
                CreateString += "<span class=\"edit\"><a href=\"categories\\edit\\" + dr["ID"] + "\">Sửa</a>";
                CreateString += " | </span><span class=\"delete\"><a class=\"delete-tag\" href=\"categories\\del\\" + dr["ID"] + "\">";
                CreateString += "Xóa</a></span></div>";
                CreateString += "</td>";
                CreateString += "<td class='description column-description'>" + dr["Descriptions"] + "</td>";
                CreateString += "<td class='slug column-slug'>" + dr["Prefix"] + "</td>";
                CreateString += "<td class='slug column-slug'>" + dr["Type"] + "</td>";
                CreateString += "<td class='posts column-posts num'><a href=\"categories\\act\\" + dr["ID"] + "\">" + dr["Enable"] + "</a></td>";
                CreateString += "</tr>";
                if (Count > 0)
                {
                    CreateCate(dr["ID"].ToString());
                }
            }
            return CreateString;
        }
    }
}
